/**  
 *	VisualGroup JavaScript prototype Classes, version 0.1
 *  (c) 2008 Bill Vison <vison_ruan@126.com>
 *
 *
/*--------------------------------------------------------------------------*/
using("Vsg.Component");
using("Vsg.Component.TriggerPanel");

/**
 * 
 * The following UrlMKey require defined
 * 	'Vsg.Component.SchedulerPanel.argurl'
 * 
 * @param {Object} $config
 */
Vsg.Component.SchedulerPanel = Ext.extend(Ext.Panel,{
	
	defaultMethod:function($config) {
		// --- defind basic defined ---
		var $_con = {
			id:"Vsg.Component.SchedulerPanel",
			collapsible:true
		};

		try {
			if ($config) {
				$_con.id = $config.id != null ? $config.id : "Vsg.Component.SchedulerPanel";
	

				for (var $i in $config) {
					$_con[$i] = $config[$i];
				}
			}

		} catch (e) {
			// -- catch default value defined ---
			alert(e);
		}
		return $_con;		
			
	},

    /**
     * constructor method
     * @param {Object} $config
     */
    constructor: function($config){

		var $attrs = this.defaultMethod($config);
		$attrs.layout = 'border';
		
		var triggerPanel = this.initTriggerPanel();

		$attrs.items = [
			this.initArgumentPanel(),
			triggerPanel
		];

		Ext.applyIf(this, $attrs);
        		
		Vsg.Component.SchedulerPanel.superclass.constructor.call(this , $attrs);

		triggerPanel.getForm().setValues(
			[{id:'code', value:'Fred. Olsen Lines'}]
		);
		        

    },
	
	/**
	 * component method
	 * @param {Object} $config
	 */
	initComponent:function($config) {
		

        Ext.applyIf(this,$config);
		
		Vsg.Component.SchedulerPanel.superclass.initComponent.call(this);
		

	},
	
	initArgumentPanel:function() {
    	var form = new Ext.form.FormPanel({
			layout:'form',
			margins:"5 5 5 0",
			split:true,
			region:'east',
			bodyStyle:'padding:5px 5px 0',
			width: 350,
        	items: [{
				xtype:'fieldset',
				title:'任务参数',
				height:368,
				layout:'border',
				collapsible: true,
				items:[{
					layout:'fit',
					region:'center',
					border:false,
					items:[{
                		id:'argumentPanel',
						xtype: 'propertygrid',
						heigh: 350,
						source: {},
        				viewConfig : {
            				forceFit: true,
            				scrollOffset: 2 // the grid will never have scrollbars
        				}
            		}]
				}]
			}]
    	});
		

		return form;			
	},
	
	initTriggerPanel:function() {
		
		// --- check the init value object exist

		var panel = new Vsg.Component.TriggerPanel({
			id: "Vsg.Component.TriggerPanel",
			margins:"5 0 5 5",
			region:'center'
		});
		
		return panel;
		
	},
	
	/**
	 * save the new schedule 
	 * @param {Object} option
	 */
	submit:function(option) {
		
		// --- get the array value ---
		var triggerFormPanel = this.getComponent('Vsg.Component.TriggerPanel') ;
		
		var argumentPanel = Ext.getCmp('argumentPanel') ;
		
		var args = argumentPanel.getSource();
		
		try {

			var triggerType = triggerFormPanel.find("name","trigType")[0].getValue();
			
			var params = {
				jobCode:triggerFormPanel.find("name","code")[0].getValue(),
				jobName:triggerFormPanel.find("name","name")[0].getValue(),
				jobClass:triggerFormPanel.find("name","jobClass")[0].getValue(),
				triggerType:triggerType
			};
			
			// --- add the args to params ---
			for (var i in args) {
				params['sca_'+i] = args[i];
			}

			
			// --- add the trigger type ---
			if (triggerType == 1) {

				var startDate = triggerFormPanel.find("name","startDate")[0].getValue();
				if (startDate) {
					params.startDateTime = startDate.format('Y-m-d') + " " + triggerFormPanel.find("name","startTime")[0].getValue();
				}
				
				var endDate = triggerFormPanel.find("name","endDate")[0].getValue();
				if (endDate) {
					params.endDateTime = tendDate.format('Y-m-d')
										+ " " + triggerFormPanel.find("name","endTime")[0].getValue();
				}
				params.repeatCount = triggerFormPanel.find("name","repeatCount")[0].getValue(); 
				params.repeatInterval = triggerFormPanel.find("name","repeatInterval")[0].getValue(); 
				

			}
			else if (triggerType == 2) {
				params.cronPattern = triggerFormPanel.find("name","cronPattern")[0].getValue();
				
			}

			// ---- send request to object ----
			Ext.Ajax.request({
   				url: option.url,
   				success:option.success,
   				failure:option.failure,
   				params: params
			});
			
		} catch (e) {
			alert('SchedulerPanel submit 出错!');
		}
		
		
	},
	
	
	initValues:function(recobj) {
		
		var triggerFormPanel = this.getComponent('Vsg.Component.TriggerPanel') ;
		
		var argumentPanel = Ext.getCmp('argumentPanel') ;
		

		// --- the type is Record ----
		if (recobj) {
			
			triggerFormPanel.find("name","code")[0].setValue( recobj.get('code') );
			triggerFormPanel.find("name","name")[0].setValue( recobj.get('name') );
			triggerFormPanel.find("name","jobClass")[0].setValue( recobj.get('jobClass') );
			
			var values = {
				code:recobj.get('code'),
				name:recobj.get('name'),
				jobClass:recobj.get('jobClass')
			};

			try {
				if (!UrlMKey['Vsg.Component.SchedulerPanel.argurl']) {
					alert("could not defined urlmkey 'Vsg.Component.SchedulerPanel.argurl' !");
					return;
				}

				// --- reset the argument defined ---
				Ext.Ajax.request({
   					url: UrlMKey['Vsg.Component.SchedulerPanel.argurl'],
   					success: function(response, opts) {
						var jsonObj = Ext.decode( response['responseText'] , true );
						argumentPanel.setSource(jsonObj.data);
					},
   					failure: function(response, opts) {
						alert('error');
					},
   					params: values
				});				
				
			} catch (e) {
				alert(e);
			}
			

		}		
	}
	
});